package com.gyx.lottery.controller.handler;

import com.gyx.lottery.common.errorcode.GlobalErrorCodeConstants;
import com.gyx.lottery.common.exception.ControllerException;
import com.gyx.lottery.common.exception.ServiceException;
import com.gyx.lottery.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger= LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value= ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e){
        //打印错误日志
        logger.error("serviceException:",e);
        return CommonResult.error(GlobalErrorCodeConstants.INITERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }
    @ExceptionHandler(value= ControllerException.class)
    public CommonResult<?> controllerException(ControllerException e){
        logger.error("controllerException:",e);
        return CommonResult.error(GlobalErrorCodeConstants.INITERNAL_SERVER_ERROR.getCode(),e.getMessage());}
    @ExceptionHandler(value= Exception.class)
    public CommonResult<?> exception(Exception e){
        logger.error("服务异常:",e);
        return CommonResult.error(GlobalErrorCodeConstants.INITERNAL_SERVER_ERROR.getCode(),e.getMessage());}
}
